package com.chezai.client.widget;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.chezai.client.R;
import com.chezai.client.global.TerminalApplication;
import com.chezai.client.utils.ScreenSizeUtils;

import butterknife.ButterKnife;

/**
 * 弹窗基类
 * Created by likunyang on 2017/9/6.
 */

abstract class BaseDialog extends Dialog {
    protected String TAG = this.getClass().getSimpleName();
    protected Context mContext;
    protected int gravity;
    private View rootView;

    public BaseDialog(@NonNull Context context) {
        this(context, R.style.normal_dialog);
    }

    public BaseDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        rootView = View.inflate(context, getDialogLayoutResId(), null);
        setContentView(rootView);
        ButterKnife.bind(this, rootView);
        initView(context);
        setListener();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(TerminalApplication.mContext).getScreenWidth() * (getwScale() == 0 ? 0.8f : getwScale()));
        lp.height = (int) (ScreenSizeUtils.getInstance(TerminalApplication.mContext).getScreenHeight() * (gethScale() == 0 ? 0.8f: gethScale()));
        lp.gravity = getGravity() == 0 ? Gravity.CENTER : getGravity();
        getWindow().setAttributes(lp);
    }

    protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        this(context);
        setCancelable(cancelable);
        setCanceledOnTouchOutside(cancelable);
        setOnCancelListener(cancelListener);
    }

    /**
     * 获取弹窗的布局资源ID
     *
     * @return
     */
    protected abstract int getDialogLayoutResId();

    /**
     * 初始化视图
     *
     * @param context
     */
    protected abstract void initView(Context context);

    protected abstract float getwScale();

    protected abstract float gethScale();

    protected abstract int getGravity();

    protected abstract void setListener();

}
